package com.si.anddos.dpadapters;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.database.Cursor;
import android.text.format.Time;

/**
 * A class for Database operations in services
 * 
 * USAGE in service:
 * 
 * DatabaseProvider db = new DatabaseProvider(Service.getContext()); db.open();
 * ArrayList<DownloadTime> list = db.getDownloadsTimes();
 * 
 * Do some code to determinate if it is necessary to start new download based
 * upon datetimes
 * 
 * Download down = db.getDownload(DownloadTime.getId()); db.close();
 * 
 * Do download...
 * 
 * 
 * 
 * @author Carlos Martinez
 * 
 */
public class DatabaseProvider {

	private AnddosDbAdapter dbHelper;

	public DatabaseProvider(Context context) {
		dbHelper = new AnddosDbAdapter(context);
	}

	public void open() {
		dbHelper.open();
	}

	public void close() {
		dbHelper.close();
	}

	// get list of DownloadTime objects contains Date and time for download and
	// id of this download in database
	public LinkedList<DownloadTime> getDownloadsTimes() {
		LinkedList<DownloadTime> downloads = new LinkedList<DownloadTime>();
		Cursor cur = dbHelper.getAllWaitingDownloads();
		while (cur.moveToNext()) {
			String time = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadDateTime));
			long id = cur.getLong(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadID));
			Time dateTime = new Time();
			dateTime.parse(time);
			downloads.add(new DownloadTime(dateTime, id));
		}
		cur.close();
		return downloads;
	}

	// get list of all downloads - deprecated dont use this shit
	@Deprecated
	public LinkedList<Download> getAllDownloads() {
		LinkedList<Download> downloads = new LinkedList<Download>();
		// USE dbHelper.getAllWaitingDownloads() for downloads which was not
		// alreadz downloaded
		Cursor cur = dbHelper.getAllDownloads();
		while (cur.moveToNext()) {
			// parsing time
			String time = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadDateTime));
			long id = cur.getLong(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadID));
			long idAccount = cur.getLong(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadAccount));
			Time dateTime = new Time();
			dateTime.parse(time);

			// get url from database
			String url = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadUrl));
			String filename = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadFilename));

			// get account
			String userName = cur.getString(
					cur.getColumnIndex(AnddosDbAdapter.colAccountUsername))
					.trim();
			String password = cur.getString(
					cur.getColumnIndex(AnddosDbAdapter.colAccountPassword))
					.trim();
			String service = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colAccountService));

			downloads.add(new Download(dateTime, url, filename, new Account(
					userName, password, service, idAccount), id));
		}
		cur.close();
		return downloads;
	}

	// get a Download object with specific Id
	public Download getDownload(long id) {
		Download download;
		Account account;
		Cursor cur = dbHelper.getDownload(id);
		if (cur != null) {
			String time = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadDateTime));
			Time dateTime = new Time();
			dateTime.parse(time);

			long idAccount = cur.getLong(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadAccount));
			String username = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colAccountUsername));
			String password = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colAccountPassword));
			String service = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colAccountService));
			int status = cur.getInt(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadStatus));
			String url = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadUrl));
			String fileName = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadFilename));
			int priority = cur.getInt(cur
					.getColumnIndex(AnddosDbAdapter.colDownloadPriority));

			download = new Download();
			account = new Account();
			account.setId(idAccount);
			account.setUserName(username);
			account.setPassword(password);
			account.setService(service);
			download.setId(id);
			download.setDateTime(dateTime);
			download.setAccount(account);
			download.setFileName(fileName);
			download.setUrl(url);
			download.setPriority(priority);
			download.setStatus(status);
		} else {
			download = null;
		}
		cur.close();
		return download;
	}

	/**
	 * @deprecated
	 */
	public boolean updateDownloadDone(long id) {
		return this.dbHelper.updateDownloadDone(id);
	}

	public boolean updateStatusDownload(long id, int status) {
		return this.dbHelper.updateStatus(id, status,
				AnddosDbAdapter.downloadsTable,
				AnddosDbAdapter.colDownloadStatus,
				AnddosDbAdapter.colDownloadID);
	}

	// Upload methods

	// get list of Upload objects from database
	public ArrayList<Upload> getAllWaitingUploads() {
		ArrayList<Upload> uploads = new ArrayList<Upload>();
		Cursor cur = dbHelper.getAllWaitingUploads();
		while (cur.moveToNext()) {
			String filePath = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colUploadFilepath));
			String fileName = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colUploadFilename));
			long id = cur.getLong(cur
					.getColumnIndex(AnddosDbAdapter.colUploadID));

			long idAccount = cur.getLong(cur
					.getColumnIndex(AnddosDbAdapter.colUploadAccount));
			String username = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colAccountUsername));
			String password = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colAccountPassword));
			String service = cur.getString(cur
					.getColumnIndex(AnddosDbAdapter.colAccountService));

			uploads.add(new Upload(filePath, fileName, new Account(username,
					password, service, idAccount), id));
		}
		cur.close();
		return uploads;
	}

	public boolean updateStatusUpload(long id, int status) {
		return this.dbHelper.updateStatus(id, status,
				AnddosDbAdapter.uploadsTable, AnddosDbAdapter.colUploadStatus,
				AnddosDbAdapter.colUploadID);
	}

	/**
	 * @deprecated
	 */
	public boolean updateUploadDone(long id) {
		return this.dbHelper.updateUploadDone(id);
	}
}
